package com.tcfvic.client.panels.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.tcfvic.client.PersonService;
import com.tcfvic.client.PersonServiceAsync;
import com.tcfvic.client.panels.widgets.addons.advancedtable.AdvancedTable;
import com.tcfvic.client.panels.widgets.addons.advancedtable.DataFilter;
import com.tcfvic.client.panels.widgets.addons.advancedtable.TableModelServiceAsync;

public class CompanyDetailsListWidget extends FlexTable {
	private final PersonServiceAsync personService = GWT
			.create(PersonService.class);
	private TextBox nameTextBox = new TextBox();
	private TextBox brandTextBox = new TextBox();
	private TextBox siteTextBox = new TextBox();
	private TextBox stateTextBox = new TextBox();
	private AdvancedTable at = new AdvancedTable();

	public CompanyDetailsListWidget(
			TableModelServiceAsync tableModelServiceAsync,
			final CompanyDetailsInputWidget rowSelectionListener) {
		super();
		at = new AdvancedTable();

		// Create a table to layout the form options
		setCellSpacing(4);
		setWidth("100%");
		FlexCellFormatter cellFormatter = getFlexCellFormatter();

		// Add a title to the form

		at.setFirstColumnVisible(false);
		at.setPageSize(4);
		// at.setHeight("300 px");
		at.setWidth("100%");
		at.addRowSelectionListener(rowSelectionListener);

		// last action to be performed
		at.setTableModelService(tableModelServiceAsync);

		nameTextBox.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[4];
					df[0] = new DataFilter("name", filter.getText());
					df[1] = new DataFilter("brand", brandTextBox.getText());
					df[2] = new DataFilter("site", siteTextBox.getText());
					df[3] = new DataFilter("state", stateTextBox.getText());
					at.applyFilters(df);
				}
			}
		});

		brandTextBox.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[4];
					df[0] = new DataFilter("name", nameTextBox.getText());
					df[1] = new DataFilter("brand", filter.getText());
					df[2] = new DataFilter("site", siteTextBox.getText());
					df[3] = new DataFilter("state", stateTextBox.getText());
					at.applyFilters(df);
				}
			}
		});

		siteTextBox.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[4];
					df[0] = new DataFilter("name", nameTextBox.getText());
					df[1] = new DataFilter("brand", brandTextBox.getText());
					df[2] = new DataFilter("site", filter.getText());
					df[3] = new DataFilter("state", stateTextBox.getText());
					at.applyFilters(df);
				}
			}
		});

		stateTextBox.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[4];
					df[0] = new DataFilter("name", nameTextBox.getText());
					df[1] = new DataFilter("brand", brandTextBox.getText());
					df[2] = new DataFilter("site", siteTextBox.getText());
					df[3] = new DataFilter("state", filter.getText());
					at.applyFilters(df);
				}
			}
		});

		// create new record button
		Button newButton = new Button("Create new");
		newButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				rowSelectionListener.createNewRecord();
			}
		});
		setWidget(0, 0, newButton);
		// create new record button

		final Button deleteButton = new Button("");
		personService.isAdminUser(new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					deleteButton.setText("Delete");
					deleteButton.addClickHandler(new ClickHandler() {
						@Override
						public void onClick(ClickEvent event) {
							personService.deleteCompany(Integer.parseInt(at
									.getSelectedRowId()),
									new AsyncCallback<Void>() {
										@Override
										public void onFailure(Throwable caught) {
											new SimpleMessagePopup(
													"The request to delete the company failed.",
													null).showPopup();
										}

										@Override
										public void onSuccess(Void result) {
											at.updateTableData();
										}
									});
						}
					});
				} else {
					deleteButton.setVisible(false);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		});
		setWidget(0, 1, deleteButton);

		setHTML(1, 0, "Company name");
		setWidget(1, 1, nameTextBox);
		setHTML(1, 2, "Brand");
		setWidget(1, 3, brandTextBox);
		setHTML(1, 4, "Site");
		setWidget(1, 5, siteTextBox);
		setHTML(1, 6, "State");
		setWidget(1, 7, stateTextBox);
		setWidget(2, 0, at);
		cellFormatter.setColSpan(2, 0, 8);

	}

	public void updateTableData() {
		at.updateTableData();
	}
}
